/**
 * 
 */

package com.shy.portal.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shy.base.controller.AbsController;
import com.shy.base.vo.AppConstants;
import com.shy.portal.entity.Role;
import com.shy.portal.entity.User;
import com.shy.portal.entity.UserRole;
import com.shy.portal.service.UserRoleService;
import com.shy.portal.service.UserService;

/**
 * <p>
 * Title: UserRoleController
 * </p>
 * <p>
 * Description:用户角色配置控制器
 * </p>
 * <p>
 * Company:ipebg
 * </p>
 * 
 * @author H2013788
 */
@Controller
@RequestMapping("/user_role")
public class UserRoleController extends AbsController {

	/**
     * log
     */
	private Logger logger = LogManager.getLogger(this.getClass());

    /**
     * userservice层
     */
    @Resource
    private UserService userService;

    /**
     * userroleservice层
     */
    @Resource
    private UserRoleService userRoleService;

    /**
     * 保存用户角色
     * @param roleIds 角色ID
     * @param userId 用户ID
     * @param response 反饋
     * @author H2201965
     * @date 2018/2/6下午3:00:49
     */
    @RequestMapping("/save_user_role")
    public void saveUserRole(Integer[] roleIds, Integer userId, HttpServletResponse response) {
        boolean result = false;
        if (null != roleIds && roleIds.length > 0) {
            List<UserRole> list = new ArrayList<>();
            UserRole userRole = null;
            User user = new User();
            user.setId(userId);
            for (Integer roleId : roleIds) {
                userRole = new UserRole();
                userRole.setUser(user);
                Role role = new Role();
                role.setId(roleId);
                userRole.setRole(role);
                userRole.setCreateTime(new Date());
                list.add(userRole);
            }
            try {
                result = userRoleService.batchSaveUserRole(list, userId);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                logger.error(AppConstants.configMap.get(AppConstants.SAVE_ERROR) + e);
            }
        }
        if (result) {
            respEasyuiSuccessResult(AppConstants.configMap.get(AppConstants.SAVE_SUCCESS), response);
        } else {
            respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.SAVE_ERROR), response);
        }
    }

}
